You Must Test Hand-Written Accessors

Mark Leighton Fisher on 2007-09-14T16:39:42

If your accessors contain any code that you wrote by hand (i.e. not IDE-generated), you must test them. There's been a debate about this subject for which I've found the counter-example to the "don't test accessors" side.

Recently, I was working on a class that had several, similarly-named accessors, which needed to be similarly-named because they were similar concepts (possibly dates but I don't remember). Sure enough, one of the accessors was modifying the underlying data for a different accessor (probably a case of cut'n'paste coding). The result was a defect in the accessor code, which would have been caught by simple tests of the accessor in question.

So: if you wrote any of the code in your accessor by hand, you must test that accessor. (This also applies to automatically-generated accessors that you modified.)